package com.xindaoapp.happypet.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.konka.imagefilter.api.BitmapFilter;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.ProgressHUD;

public class ImageFilterActivity extends Activity implements OnClickListener {

    private ImageView photo, im01, im02, im03, im04, im05, im06, im07;
    private LinearLayout image01, image02, image03, image04, image05, image06, image07, image08,
            image09, image10, image11, image12, image13, image14;
    // private Bitmap b01, b02, b03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14;
    BitmapFilter bitmapfilter = null;
    private Bitmap bmp, sBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_image_filter);

        photo = (ImageView) findViewById(R.id.iv_photo);

        image01 = (LinearLayout) findViewById(R.id.image01);
        image02 = (LinearLayout) findViewById(R.id.image02);
        image03 = (LinearLayout) findViewById(R.id.image03);
        image04 = (LinearLayout) findViewById(R.id.image04);
        image05 = (LinearLayout) findViewById(R.id.image05);
        image06 = (LinearLayout) findViewById(R.id.image06);
        image07 = (LinearLayout) findViewById(R.id.image07);
        image08 = (LinearLayout) findViewById(R.id.image08);
        image09 = (LinearLayout) findViewById(R.id.image09);
        image10 = (LinearLayout) findViewById(R.id.image10);
        image11 = (LinearLayout) findViewById(R.id.image11);
        image12 = (LinearLayout) findViewById(R.id.image12);
        image13 = (LinearLayout) findViewById(R.id.image13);
        image14 = (LinearLayout) findViewById(R.id.image14);
        image01.setOnClickListener(this);
        image02.setOnClickListener(this);
        image03.setOnClickListener(this);
        image04.setOnClickListener(this);
        image05.setOnClickListener(this);
        image06.setOnClickListener(this);
        image07.setOnClickListener(this);
        image08.setOnClickListener(this);
        image09.setOnClickListener(this);
        image10.setOnClickListener(this);
        image11.setOnClickListener(this);
        image12.setOnClickListener(this);
        image13.setOnClickListener(this);
        image14.setOnClickListener(this);


        im01 = (ImageView) findViewById(R.id.im01);
        im02 = (ImageView) findViewById(R.id.im02);
        im03 = (ImageView) findViewById(R.id.im03);
        im04 = (ImageView) findViewById(R.id.im04);
        im05 = (ImageView) findViewById(R.id.im05);
        im06 = (ImageView) findViewById(R.id.im06);
        im07 = (ImageView) findViewById(R.id.im07);

        bitmapfilter = new BitmapFilter();

        final String image = getIntent().getStringExtra("image");
        // bmp = setImageBitmap(image);
        bmp = setImageBitmap(image);
        photo.setImageBitmap(bmp);

        findViewById(R.id.save).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try {
                    photo.setClickable(false);
                    saveMyBitmap(image);
                    photo.setClickable(true);
                    setResult(111);
                    finish();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        init();

        // new Thread() {
        // @Override
        // public void run() {
        // };
        // }.start();
        // String image = getIntent().getStringExtra("image");
        // new processImageTask(ImageActivity.this, bitmapfilter, image).execute();
    }

    public void saveMyBitmap(String bitName) throws IOException {
        ProgressHUD dialog =
                ProgressHUD.show(ImageFilterActivity.this, "正在保存...", true, true, null);
        File f = new File(bitName);
        f.createNewFile();
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (sBitmap != null) {
            sBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        }
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        dialog.dismiss();
    }


    private void init() {

    }

    private Bitmap setImageBitmap(String realPath) {
        Bitmap bmp = BitmapFactory.decodeFile(realPath);
        int degree = readPictureDegree(realPath);

        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        Bitmap resizedBitmap =
                Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth() - 20, bmp.getHeight() - 30, matrix,
                        true);
        return resizedBitmap;
    }

    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation =
                    exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    @Override
    public void onClick(View v) {
        ProgressHUD dialog =
                ProgressHUD.show(ImageFilterActivity.this, "正在渲染中...", true, true, null);
        if (sBitmap != null && v.getId() != R.id.save) {
            sBitmap.recycle();
            sBitmap = null;
        }
        switch (v.getId()) {
            case R.id.image01:
                photo.setImageBitmap(bmp);
                dialog.dismiss();
                break;
            case R.id.image02:// LOMO效果
                // sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.LOMO_STYLE);
                // photo.setImageBitmap(sBitmap);
                setChange(bmp, bitmapfilter.LOMO_STYLE);
                dialog.dismiss();
                break;
            case R.id.image03:// 黑白
                // sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.GRAY_STYLE);
                // photo.setImageBitmap(sBitmap);
                setChange(bmp, bitmapfilter.GRAY_STYLE);
                dialog.dismiss();
                break;
            case R.id.image04:// 怀旧效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.OLD_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image05:// 冰冻效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.ICE_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image06:// 连环画效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.CARTON_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image07:// 铸融效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.MOLTEN_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image08:// 哈哈效果
                // sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.HAHA_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image09:// 羽化效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.ECLOSION_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image10:// 浮雕效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.RELIEF_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image11:// 油画效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.OIL_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image12:// 反色效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.INVERT_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image13:// 版画
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.BLOCK_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;
            case R.id.image14:// 光照效果
                sBitmap = bitmapfilter.changeStyle(bmp, bitmapfilter.LIGHT_STYLE);
                // setChange(bmp, bitmapfilter.LIGHT_STYLE);
                photo.setImageBitmap(sBitmap);
                dialog.dismiss();
                break;

            default:
                break;
        }
    }

    private void setChange(final Bitmap bmp2, final int lightStyle) {
        new Thread() {
            @Override
            public void run() {
                sBitmap = bitmapfilter.changeStyle(bmp2, lightStyle);
                handler.sendEmptyMessage(0);
            };
        }.start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            photo.setImageBitmap(sBitmap);
        };
    };


}
